package com.easy.frame.common.mq.rabbit.confirm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 消息不可达监听
 *
 * @author 86131
 */
@Slf4j
@Component
public class RetrunCallBack implements RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        //指定 ReturnCallback
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("correlationId:{}" + message.getMessageProperties().getCorrelationId());
        System.out.println("replyText:{}" + replyText);
        System.out.println("消息内容:{}" + new String(message.getBody()));
        System.out.println("replyCode:{}" + replyCode);
        System.out.println("交换机:{}" + exchange);
        System.out.println("routingKey:{}" + routingKey);
        System.out.println("需要更新数据库日志表得消息记录为不可达");
    }
}
